package com.personal.website.lottery.table

import java.util
import scala.collection.JavaConverters._

/**
  * Created by Gavin on 2016/4/3.
  */
class ElevenDigitTable(issue: Int,redBalls: Seq[Int], blueBall: Int) extends Table(issue,redBalls,blueBall){
  override def createRecord(): Seq[Int] = createMainRecord() ++ createMiss() ++ create012()

  def createMainRecord() = ElevenDigitTable.ALL_EXISTS_BALLS.map(x => Seq(if(redBalls.exists(_ == x)) x else DEFAULT_NON_EXISTS_VALUE,if(redBalls.exists(_ == x + 11)) x + 11 else DEFAULT_NON_EXISTS_VALUE,if(redBalls.exists(_ == x + 22)) x + 22 else DEFAULT_NON_EXISTS_VALUE)).flatten
  def createMiss() = ElevenDigitTable.ALL_EXISTS_BALLS.map(x => if(redBalls.count(y => y % 11 == x % 11) > 0) x else DEFAULT_NON_EXISTS_VALUE)
  def create012() = Seq(3,6,9,1,4,7,10,2,5,8,11).map(x => if(redBalls.count(y => y % 11 == x % 11) > 0) x else DEFAULT_NON_EXISTS_VALUE)
  override def toString: String = Seq(Seq(issue),createRecord()).flatten.mkString(";")
}

object ElevenDigitTable {
  def apply(issue: Int,redBalls: Seq[Int], blueBall: Int) = new ElevenDigitTable(issue,redBalls,blueBall)
  def apply(issue: Int,allBalls: Seq[Int]) = new ElevenDigitTable(issue,allBalls.init,allBalls.last)
  def newInstance(issue: Integer,redBalls: util.List[Integer],blueBall: Integer) = new ElevenDigitTable(issue.toInt, redBalls.asScala.map(_.toInt),blueBall.toInt)

  val ALL_EXISTS_BALLS = (1 to 11)
}